package org.xurui.sxl.protocol.command.impl;

import org.jetlinks.core.message.DeviceMessage;
import org.jetlinks.core.message.codec.FromDeviceMessageContext;
import org.jetlinks.core.message.property.ReportPropertyMessage;
import org.xurui.sxl.protocol.command.CmdCodec;
import org.xurui.sxl.protocol.enums.CommandEnum;
import org.xurui.sxl.protocol.utils.NumberUtils;
import org.xurui.sxl.protocol.utils.SxlMessageUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * WIFI 定位
 */
public class CmdCodec_Wifimac implements CmdCodec {
    @Override
    public boolean supportsCmd(String cmd) {
        return CommandEnum.WIFIMAC.name().equals(cmd);
    }

    @Override
    public boolean supportsRelayCmd() {
        return false;
    }

    @Override
    public List<DeviceMessage> parsePayload(String payload, FromDeviceMessageContext context) {
        //设备上报
        //格式： *XX,YYYYYYYYYYYYYYY,WIFIMAC,HHMMSS,Signal,SV,BAT,NUM,MAC,RSSI,MAC,RSSI,,,DDMMYY,FFFFFFFF#
        //示例1：*TE,7893267561,WIFIMAC,130305,90,10,90,1,B4:0B:44:CA:D4:8A,-90,220902,FFFFFFFF#
        //示例2：*TE,7893267561,WIFIMAC,130305,90,10,90,2,B3:0B:44:CA:D4:8A,-90,B3:0B:44:CA:D4:8A,-90,220902,FFFFFFFF#
        //回复格式：*XX,YYYYYYYYYYYYYYY,V4,WIFIMAC,seq,HHMMSS#
        //回复示例：TE,866104040001458,V4,WIFIMAC,130305,130305#
        //解析数据为属性上报
        ReportPropertyMessage report = ReportPropertyMessage.create();
        String[] payloadArr = payload.replaceAll("\\*","").split(",");
        String deviceId = payloadArr[1];
        report.setDeviceId(deviceId);
        report.setCode(CommandEnum.WIFIMAC.name());
        report.setMessageId(deviceId+"_"+payloadArr[3]);
        report.setTimestamp(System.currentTimeMillis());
        Map<String, Object> properties = new HashMap<>();
        properties.put("uptime",payloadArr[3]);
        properties.put("signal", NumberUtils.parseInt(payloadArr[4]));
        properties.put("sv",NumberUtils.parseInt(payloadArr[5]));
        properties.put("bat",NumberUtils.parseInt(payloadArr[6]));
        int macNumber = NumberUtils.parseInt(payloadArr[7]);//数量，1-6，最多 6 个
        List<Map> macs = new ArrayList<>();
        for(int i=0;i<macNumber;i++){
            Map mac = new HashMap();
            mac.put("mac",payloadArr[8+i*2]);
            mac.put("rssi",payloadArr[9+i*2]);
            macs.add(mac);
        }
        properties.put("macinfo",macs);
        properties.put("upday",payloadArr[8+macNumber*2]);

        String trackerStatusHex = payloadArr[8+macNumber*2+1];
        Map<String,Object> trackerStatusMap = parseTrackerStatus(trackerStatusHex);
        properties.put("trackerstatus",trackerStatusMap);
        report.setProperties(properties);
        //回复设备数据
        //TODO 暂时没找到设备回复消息的方法
//        String day= BinaryUtils.getDateFormat();
//        String reply="*"+payloadArr[0]+","+deviceId+",V4,"+CommandEnum.WIFIMAC.name()+","+payloadArr[3]+","+day+"#";
//        context.getSession()
//            .send(EncodedMessage
//                .simple(Unpooled.wrappedBuffer(reply.getBytes())))
//            .subscribe();
        SxlMessageUtils.bulidAndSend2DeviceV4Msg(context,payloadArr[0],deviceId,CommandEnum.WIFIMAC.name(),payloadArr[3]);
        return genDeviceMessage(report);
    }

}
